package com.aihna.mall.service.impl;

import com.aihna.common.core.utils.MapstructUtils;
import com.aihna.common.mybatis.core.page.TableDataInfo;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.aihna.mall.domain.bo.StoreProductAttrBo;
import com.aihna.mall.domain.vo.StoreProductAttrVo;
import com.aihna.mall.domain.StoreProductAttr;
import com.aihna.mall.mapper.StoreProductAttrMapper;
import com.aihna.mall.service.IStoreProductAttrService;
import com.aihna.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品属性Service业务层处理
 *
 * @author Joseph.Chen
 * @date 2024-03-29
 */
@RequiredArgsConstructor
@Service
public class StoreProductAttrServiceImpl implements IStoreProductAttrService {

    private final StoreProductAttrMapper baseMapper;

    /**
     * 查询商品属性
     */
    @Override
    public StoreProductAttrVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品属性列表
     */
    @Override
    public TableDataInfo<StoreProductAttrVo> queryPageList(StoreProductAttrBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StoreProductAttr> lqw = buildQueryWrapper(bo);
        Page<StoreProductAttrVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品属性列表
     */
    @Override
    public List<StoreProductAttrVo> queryList(StoreProductAttrBo bo) {
        LambdaQueryWrapper<StoreProductAttr> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StoreProductAttr> buildQueryWrapper(StoreProductAttrBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StoreProductAttr> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, StoreProductAttr::getProductId, bo.getProductId());
        lqw.like(StringUtils.isNotBlank(bo.getAttrName()), StoreProductAttr::getAttrName, bo.getAttrName());
        lqw.eq(bo.getType() != null, StoreProductAttr::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增商品属性
     */
    @Override
    public Boolean insertByBo(StoreProductAttrBo bo) {
        StoreProductAttr add = MapstructUtils.convert(bo, StoreProductAttr.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品属性
     */
    @Override
    public Boolean updateByBo(StoreProductAttrBo bo) {
        StoreProductAttr update = MapstructUtils.convert(bo, StoreProductAttr.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StoreProductAttr entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品属性
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<StoreProductAttrVo> getListByProductIdAndType(Long productId, Integer type) {
        StoreProductAttrBo bo = new StoreProductAttrBo();
        bo.setProductId(productId);
        bo.setType(type);
        LambdaQueryWrapper<StoreProductAttr> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public void saveBatch(List<StoreProductAttrBo> attrList) {
        List<StoreProductAttr> add = MapstructUtils.convert(attrList, StoreProductAttr.class);
        baseMapper.insertBatch(add, 1000);
    }

    @Override
    public Boolean deleteByProductIdAndType(Long productId, Integer type) {
        LambdaUpdateWrapper<StoreProductAttr> luw = Wrappers.lambdaUpdate();
        luw.set(StoreProductAttr::getDelFlag, 1);
        luw.eq(StoreProductAttr::getProductId, productId);
        luw.eq(StoreProductAttr::getType, type);
        return baseMapper.delete(luw) > 0;
    }

    @Override
    public void saveOrUpdateBatch(List<StoreProductAttrBo> attrUpdateList) {
        List<StoreProductAttr> add = MapstructUtils.convert(attrUpdateList, StoreProductAttr.class);
        baseMapper.insertOrUpdateBatch(add, 1000);
    }
}
